﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Data;

namespace Carriage
{
    public class UPSClass
    {
        private string path = System.Web.HttpContext.Current.Server.MapPath("~/UPSPrice.xml");

        private double _weight = 0;
        /// <summary>
        /// 货物净重(单位:克) 
        /// </summary>
        internal double weight
        {
            get
            {
                return _weight;
            }
            set
            {
                _weight = value;
            }
        }

        private double _weightrate = 1.3;
        /// <summary>
        /// 毛重与净重的比率
        /// </summary>
        public double weightrate
        {
            get
            {
                return _weightrate;
            }
            set
            {
                _weightrate = value;
            }
        }

        private int _areaNumber;
        /// <summary>
        /// 所在ｄｈｌ分布区号
        /// </summary>
        public int AreaNumber
        {
            get
            {
                return _areaNumber;
            }
            set
            {
                _areaNumber = value;
            }
        }

        private double _Fuelcosts = 1.175;
        /// <summary>
        /// 燃油费系数
        /// </summary>
        public double Fuelcosts
        {
            get
            {
                return _Fuelcosts;
            }
            set
            {
                _Fuelcosts = value;
            }
        }

        private double _Surcharge = 50;
        /// <summary>
        /// 附加费
        /// </summary>
        public double Surcharge
        {
            get
            {
                return _Surcharge;
            }
            set
            {
                _Surcharge = value;
            }
        }


        /// <summary>
        /// 获取UPS的运费
        /// 所需条件:
        /// １.目的国家所在ups分布区区号
        /// ２.重量
        /// ３.读取所在区的价格信息
        /// </summary>
        /// <returns></returns>
        internal double GetCarriage()
        {
            double rlt = 0;
            double value1 = 0;//起重标准
            double value2 = 0;//续重标准
            double value3 = 1;//折扣

            int flag = 1;//计费方式.　１代表按0.5公斤算,２代表用ＫＧ算
            double cw = (double)(this.weight * this.weightrate);//货物的毛重
            //cw = double.Round(cw,2);
            double cwKg = cw / 1000;
            //cwKg = double.Round(cwKg,2);

            
            #region 取超重包裹的最低消费
            XmlDataDocument upsdata = new XmlDataDocument();//新建一个ｘｍｌ数据文档对象
            upsdata.DataSet.ReadXml(path);
            DataView upsDv = upsdata.DataSet.Tables[0].DefaultView;
            //objDv.Sort = " order by Weight desc ";//排行方法
            upsDv.RowFilter = "Flag=3";
            double aboveValue = double.Parse(upsDv[0]["No" + this.AreaNumber.ToString()].ToString().Trim());
            double aboveWeight = double.Parse(upsDv[0]["Weight"].ToString().Trim());
            upsdata = null;//清空对XML数据的操作
            #endregion

            if (cwKg < aboveWeight  )
            {
                XmlDataDocument dhldata = new XmlDataDocument();//新建一个ｘｍｌ数据文档对象
                dhldata.DataSet.ReadXml(path);
                DataView objDv = dhldata.DataSet.Tables[0].DefaultView;
                objDv.Sort = "Weight asc";//排行方法
                objDv.RowFilter = "Weight>=" + cwKg.ToString().Trim();


                double maxvalue = 0;
                DataRowView maxrowview = objDv[0];
                foreach (DataRowView rowview in objDv)
                {
                    if (double.Parse(rowview["Weight"].ToString().Trim()) <= double.Parse(maxrowview["Weight"].ToString().Trim()))
                    {
                        maxrowview = rowview;
                    }
                }

                //获取首重和续重 以及计费方式
                value1 = double.Parse(maxrowview["No" + this.AreaNumber.ToString()].ToString().Trim());
                value2 = double.Parse(maxrowview["Extracost"].ToString().Trim());
                flag = int.Parse(maxrowview["Flag"].ToString().Trim());
                value3 = double.Parse(maxrowview["Discount"].ToString().Trim());

                dhldata = null;//清空对XML数据的操作

                if (flag == 1)
                {
                    //区ups标准价
                    rlt = value1;
                }
                else
                    return 0;
            }
            else
            {
                upsdata = new XmlDataDocument();//新建一个ｘｍｌ数据文档对象
                upsdata.DataSet.ReadXml(path);
                upsDv = upsdata.DataSet.Tables[0].DefaultView;
                //objDv.Sort = " order by Weight desc ";//排行方法
                upsDv.RowFilter = "Flag=2";
                value1 = double.Parse(upsDv[0]["No" + this.AreaNumber.ToString()].ToString().Trim());
                value2 = double.Parse(upsDv[0]["Extracost"].ToString().Trim());
                value3 = double.Parse(upsDv[0]["Discount"].ToString().Trim());
                upsdata = null;//清空对XML数据的操作

                int unit = 0;
                //按kg计算
                double rvalue = cwKg % 1;
                if (rvalue > 0)
                    unit = (int)cwKg + 1;
                else
                    unit = (int)cwKg;
                rlt = unit * value1;
                if (rlt < aboveValue)
                    rlt = aboveValue;
            }
            return rlt * value3 * _Fuelcosts + _Surcharge;//标准运费乘折扣
        }
    }
}
